package com.health.trans.writer;

import com.health.trans.annotation.Writer;
import com.health.trans.kit.WriterConfig;
import com.health.trans.processor.writer.CsWriter;
import com.health.trans.service.AsyncDataService;
import com.health.trans.util.DataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.EntityManagerFactory;

/**
 * @author nqlz
 */
@Slf4j
@Writer
public class SyncDataWriter implements CsWriter {

    @Autowired
    private AsyncDataService asyncDataService;

    @Override
    public <T> ItemWriter<T> getWriter(WriterConfig<T> writerConfig) {
        EntityManagerFactory entityManagerFactory = DataSourceUtil.getEntityManagerFactory(writerConfig.getSourceName());
        return new CsJpaItemWriter<>(writerConfig, asyncDataService,entityManagerFactory);
    }
}
